package com.awplanet.sitegen.template;

import com.awplanet.sitegen.constant.Constant;
import com.awplanet.sitegen.data.*;
import com.awplanet.sitegen.utils.RandomUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * User: a_polyakov
 */
public class AttrTemplate extends AbstractTemplate {
	private String name;

	public AttrTemplate(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	@Override
	public void write(OutputStream outputStream, Map<String, String> attrs, Map<String, Object> hideAttr) throws IOException {
		if ("include.pageButton.html".equals(name)) {
			Template pageButtonTemplate = (Template) hideAttr.get("pageButtonTemplate");
			Template pageSelectButtonTemplate = (Template) hideAttr.get("pageSelectButtonTemplate");
			String pageButtonUrl = (String) hideAttr.get("pageButtonUrl");
			int pageButtonPage = (Integer) hideAttr.get("pageButtonPage");
			int pageButtonSize = (Integer) hideAttr.get("pageButtonSize");
			int pageButtonPageSize = (Integer) hideAttr.get("pageButtonPageSize");
			if (pageButtonSize > pageButtonPageSize && pageButtonPageSize > 0) {
				int pageCount = pageButtonSize / pageButtonPageSize + ((pageButtonSize % pageButtonPageSize > 0) ? 1 : 0);
				for (int index = 1; index <= pageCount; index++) {
					attrs.put("page.url", pageButtonUrl + ((index == 1) ? "" : index) + ".html");
					attrs.put("page.index", String.valueOf(index));
					if (pageButtonPage == index) {
						pageSelectButtonTemplate.write(outputStream, attrs, hideAttr);
					} else {
						pageButtonTemplate.write(outputStream, attrs, hideAttr);
					}
				}
				attrs.remove("page.url");
				attrs.remove("page.index");
			}
		} else if ("include.indexItem.html".equals(name)) {
			Template indexItemTemplate = (Template) hideAttr.get("indexItemTemplate");
			List<Country> list = (List<Country>) hideAttr.get("indexItemList");
			for (Country country : list) {
				attrs.putAll(country);
				attrs.put("country.url", country.getFileName() + "/index.html");
				indexItemTemplate.write(outputStream, attrs, hideAttr);
			}
		} else if ("randomText".equals(name)) {
			Random random= (Random) hideAttr.get("random");
			List<String> textList= (List<String>) hideAttr.get("textList");
			int minRandomText=(Integer)hideAttr.get("minRandomText");
			int maxRandomText=(Integer) hideAttr.get("maxRandomText");
			minRandomText = RandomUtils.nextRandomInt(random, minRandomText, maxRandomText);
			for (int i = 0; i < minRandomText; i++) {
				int index = random.nextInt(textList.size());
				String text = textList.get(index);
				outputStream.write(text.getBytes(Constant.UTF_8));
				outputStream.write("<br>".getBytes(Constant.UTF_8));
			}
		} else if ("include.countryItem.html".equals(name)){
			Template countryItemTemplate= (Template) hideAttr.get("countryItemTemplate");
			List<City> countryItemList= (List<City>) hideAttr.get("countryItemList");
			for (City city:countryItemList){
				attrs.putAll(city);
				attrs.put("index.url", "../index.html");
				attrs.put("country.url", "index.html");
				attrs.put("city.url", city.getFileName() + ".html");
				countryItemTemplate.write(outputStream, attrs, hideAttr);
			}
		} else if ("include.cityItem.html".equals(name)){
			Template cityItemTemplate= (Template) hideAttr.get("cityItemTemplate");
			List<Department> cityItemList= (List<Department>) hideAttr.get("cityItemList");
			for (Department department:cityItemList){
				attrs.putAll(department);
				hideAttr.put("department", department);
				cityItemTemplate.write(outputStream, attrs, hideAttr);
			}
		} else if ("include.departmentWorkTime.html".equals(name)) {
			Template departmentWorkTimeTemplate= (Template) hideAttr.get("departmentWorkTimeTemplate");
			Department department = (Department) hideAttr.get("department");
			for (DepartmentWorkTime departmentWorkTime : department.getWorkTimeList()) {
				attrs.putAll(departmentWorkTime);
				departmentWorkTimeTemplate.write(outputStream, attrs, hideAttr);
			}
		} else if ("include.departmentService.html".equals(name)){
			Template departmentServiceTemplate= (Template) hideAttr.get("departmentServiceTemplate");
			Department department = (Department) hideAttr.get("department");
			for (DepartmentService departmentService : department.getServiceList()) {
				attrs.putAll(departmentService);
				departmentServiceTemplate.write(outputStream, attrs, hideAttr);
			}
		}else if("include.randomCityLink.html".equals(name)){
			// TODO use attr random.county.name random.county.url and city
			Random random= (Random) hideAttr.get("random");
			Template randomCityLinkTemplate= (Template) hideAttr.get("randomCityLinkTemplate");
			int minRandomLink=(Integer)hideAttr.get("minRandomLink");
			int maxRandomLink=(Integer)hideAttr.get("maxRandomLink");
			Model model= (Model) hideAttr.get("model");
			minRandomLink = RandomUtils.nextRandomInt(random, minRandomLink, maxRandomLink);
			for (int i = 0; i < minRandomLink; i++) {
				int index = random.nextInt(model.getCountryMap().size());
				Iterator<Country> iterator = model.getCountryMap().values().iterator();
				while (index > 0) {
					iterator.next();
					index--;
				}
				Country country = iterator.next();
				attrs.putAll(country);
				index = random.nextInt(country.getCityMap().size());
				Iterator<City> iterator2 = country.getCityMap().values().iterator();
				while (index > 0) {
					iterator2.next();
					index--;
				}
				City city = iterator2.next();
				attrs.putAll(city);
				attrs.put("country.url", "../" + country.getFileName() + "/index.html");
				attrs.put("city.url", "../" + country.getFileName() + "/" + city.getFileName() + ".html");
				randomCityLinkTemplate.write(outputStream, attrs, hideAttr);
			}
			Country country= (Country) hideAttr.get("country");
			City city= (City) hideAttr.get("city");
			attrs.putAll(country);
			attrs.putAll(city);
			attrs.put("country.url", "index.html");
			attrs.put("city.url", city.getFileName() + ".html");
		}
		else {
			String value = attrs.get(name);
			if (value == null)
				value = "${" + name + "}";
			outputStream.write(value.getBytes(Constant.UTF_8));
		}
	}
}
